<template>
  <div class="app-container">
    <header class="app-header">
      <h1 class="app-title">Electron-Vue3 应用</h1>
      <p class="app-subtitle">点击下方按钮执行外部程序</p>
    </header>

    <main class="app-main">
      <div class="control-panel">
        <button class="execute-btn" @click="runExternalProgram" :disabled="isLoading">
          <span v-if="!isLoading">执行程序</span>
          <span v-else class="loading-spinner"></span>
        </button>

        <div class="input-group">
          <label for="args">参数 (空格分隔):</label>
          <input id="args" v-model="inputArgs" type="text" placeholder="例如: arg1 arg2 arg3" />
        </div>
      </div>

      <div class="output-panel">
        <div class="status-indicator" :class="statusClass">
          {{ statusMessage }}
        </div>

        <div v-if="output" class="output-content">
          <h3>程序输出:</h3>
          <pre>{{ output }}</pre>
        </div>

        <div v-if="error" class="error-content">
          <h3>错误信息:</h3>
          <pre>{{ error }}</pre>
        </div>
      </div>
    </main>

    <footer class="app-footer">
      <p>Electron-Vue3 应用 &copy; {{ new Date().getFullYear() }}</p>
    </footer>                                                                                                                                                                                                                                                                            
  </div>
</template>

<script setup>
import { ref, computed } from "vue";

const output = ref(null);
const error = ref(null);
const isLoading = ref(false);
const inputArgs = ref("arg1 arg2");

const statusMessage = computed(() => {
  if (isLoading.value) return "执行中...";
  if (error.value) return "执行失败";
  if (output.value) return "执行成功";
  return "准备就绪";
});

const statusClass = computed(() => {
  return {
    "status-ready": !isLoading.value && !output.value && !error.value,
    "status-loading": isLoading.value,
    "status-success": !isLoading.value && output.value,
    "status-error": !isLoading.value && error.value,
  };
});

const runExternalProgram = async () => {
  try {
    isLoading.value = true;
    output.value = null;
    error.value = null;

    const exeRelativePath = import.meta.env.DEV ? "public/test.exe" : "test.exe";
    const args = inputArgs.value.split(" ").filter((arg) => arg.trim());

    const result = await window.electronAPI.executeProgram(
      exeRelativePath,
      args
    );

    if (!result.success) {
      throw new Error(result.error || "程序执行失败");
    }

    try {
      output.value = JSON.parse(result.output);
    } catch {
      output.value = result.output;
    }

  } catch (err) {
    console.error("执行出错:", err);
    error.value = err.message;
  } finally {
    isLoading.value = false;
  }
};
</script>

<style>
:root {
  --primary-color: #4a6bff;
  --success-color: #28a745;
  --error-color: #dc3545;
  --warning-color: #ffc107;
  --text-color: #333;
  --light-bg: #f8f9fa;
  --border-color: #dee2e6;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.6;
  color: var(--text-color);
  background-color: #f5f7ff;
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.app-header {
  text-align: center;
  margin-bottom: 2rem;
  padding: 1rem;
  background: linear-gradient(135deg, var(--primary-color), #6c5ce7);
  color: white;
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.app-title {
  font-size: 2.2rem;
  margin-bottom: 0.5rem;
}

.app-subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.control-panel {
  background: white;
  padding: 1.5rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.execute-btn {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 0.8rem 1.5rem;
  font-size: 1rem;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
}

.execute-btn:hover {
  background-color: #3a5bef;
  transform: translateY(-2px);
}

.execute-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
  transform: none;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.input-group label {
  font-weight: 500;
}

.input-group input {
  padding: 0.8rem;
  border: 1px solid var(--border-color);
  border-radius: 5px;
  font-size: 1rem;
}

.output-panel {
  background: white;
  padding: 1.5rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.status-indicator {
  padding: 0.8rem;
  border-radius: 5px;
  margin-bottom: 1.5rem;
  font-weight: 500;
  text-align: center;
}

.status-ready {
  background-color: var(--light-bg);
  color: var(--text-color);
}

.status-loading {
  background-color: var(--warning-color);
  color: #333;
}

.status-success {
  background-color: var(--success-color);
  color: white;
}

.status-error {
  background-color: var(--error-color);
  color: white;
}

.output-content,
.error-content {
  margin-top: 1rem;
}

.output-content h3,
.error-content h3 {
  margin-bottom: 0.5rem;
  color: var(--primary-color);
}

.error-content h3 {
  color: var(--error-color);
}

pre {
  background-color: var(--light-bg);
  padding: 1rem;
  border-radius: 5px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.app-footer {
  text-align: center;
  margin-top: 2rem;
  padding: 1rem;
  color: #666;
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .app-container {
    padding: 15px;
  }

  .app-title {
    font-size: 1.8rem;
  }
}
</style>